# -*-coding:utf-8 -*-
from django.db import models
from django.contrib import admin
from forfood.customer.models import Customer
from forfood.restaurant.models import Restaurant
from forfood.menu.models import MenuItem
from time import time
from time import ctime
from time import strftime
from time import localtime

class Order(models.Model):
    customer = models.ForeignKey(Customer)
    restaurant = models.ForeignKey(Restaurant)
    status = models.IntegerField(default=0)
    create_time = models.IntegerField(default=int(time()))
    c_confirm_time = models.IntegerField(default=int(time()))
    r_confirm_time = models.IntegerField(default=int(time()))
    send_time = models.IntegerField(default=int(time()))
    complete_time = models.IntegerField(default=int(time()))
    
    def __unicode__(self):
        return u'%s'%self.id
    
    def get_items(self):
        return self.orderitem_set.all()
    def get_count(self):
        count = 0
        for i in self.get_items():
            count = count + i.count
        return count
    def get_total(self):
        total = 0
        for i in self.get_items():
            total = total + i.get_total()
        return total
    def get_status(self):
        if self.status == 0:
            return "未下单"
        elif self.status == 1:
            return "店家未确认"
        elif self.status == 2:
            return "正在备餐"
        elif self.status == 3:
            return "已送出"
        elif self.status == 4:
            return "已完成"
        else:
            return "状态错误"
    def set_time_by_status(self):
        if self.status == 0:
            self.create_time = int(time())
        elif self.status == 1:
            self.c_confirm_time = int(time())
        elif self.status == 2:
            self.r_confirm_time = int(time())
        elif self.status == 3:
            self.send_time = int(time())
        elif self.status == 4:
            self.complete_time = int(time())
        self.save()
    def get_time(self, status):
        if status == 0:
            return  strftime('%Y-%m-%d %H:%M:%S', localtime(self.create_time))
        if status == 1:
            return  strftime('%Y-%m-%d %H:%M:%S', localtime(self.c_confirm_time))
        if status == 2:
            return  strftime('%Y-%m-%d %H:%M:%S', localtime(self.r_confirm_time))
        if status == 3:
            return  strftime('%Y-%m-%d %H:%M:%S', localtime(self.send_time))
        if status == 4:
            return  strftime('%Y-%m-%d %H:%M:%S', localtime(self.complete_time))
    def get_time0(self):
        return self.get_time(0)
    def get_time1(self):
        if self.status < 1:
            return "用户未下单"
        return self.get_time(1)
    def get_time2(self):
        if self.status < 2:
            return "餐馆未确认订单"
        return self.get_time(2)
    def get_time3(self):
        if self.status < 3:
            return "订单未配送"
        return self.get_time(3)
    def get_time4(self):
        if self.status < 4:
            return "订单未确认送达"
        return self.get_time(4)
    def get_button_value(self):
        rv = ""
        if self.status == 1:
            rv = "接受订单，开始备餐 "
        if self.status == 2:
            rv = "备餐完毕，开始配送 "
        if self.status == 3:
            rv = "完成"
        return rv
class OrderAdmin(admin.ModelAdmin):
    pass
admin.site.register(Order, OrderAdmin)

class OrderItem(models.Model):
    order = models.ForeignKey(Order)
    item = models.ForeignKey(MenuItem)
    count = models.IntegerField()
    
    def get_price(self):
        return self.item.price
    def get_total(self):
        return self.get_price() * self.count
    
class OrderItemAdmin(admin.ModelAdmin):
    pass
admin.site.register(OrderItem, OrderItemAdmin)